﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;

namespace Gtkj.CMS.Common
{
    public class ImageCutter
    {
        static public Image CutAvatar(Image SrcImg, int offsetX, int offsetY, int CutWidth, int CutHeight, int DesWidth = 80, int DesHeight = 80)
        {
            if (offsetX < 0)
            {
                offsetX = 0;
            }
            if (offsetX > SrcImg.Width - 1)
            {
                offsetX = SrcImg.Width - 1;
            }

            if (offsetY < 0)
            {
                offsetY = 0;
            }
            if (offsetY > SrcImg.Height - 1)
            {
                offsetY = SrcImg.Height - 1;
            }

            if (offsetX + CutWidth > SrcImg.Width - 1)
            {
                CutWidth = SrcImg.Width - 1 - offsetX;
            }

            if (offsetY + CutHeight > SrcImg.Height - 1)
            {
                CutHeight = SrcImg.Height - 1 - offsetY;
            }

            // ＝＝＝裁剪图片＝＝＝
            Rectangle CutRect = new Rectangle(offsetX, offsetY, CutWidth, CutHeight);
            Bitmap oldBitmap = new Bitmap(SrcImg);
            PixelFormat format = oldBitmap.PixelFormat;
            Bitmap CutBitmap = oldBitmap.Clone(CutRect, format);
            oldBitmap.Dispose();

            // ＝＝＝缩放图片＝＝＝

            Bitmap DesBitmap = new Bitmap(DesWidth, DesHeight);
            Graphics grap = Graphics.FromImage(DesBitmap);
            Rectangle oldRect = new Rectangle(0, 0, CutBitmap.Width, CutBitmap.Height);
            Rectangle newRect = new Rectangle(0, 0, DesWidth, DesHeight);
            grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            grap.DrawImage(CutBitmap, newRect, oldRect, GraphicsUnit.Pixel);
            CutBitmap.Dispose();

            return DesBitmap;
        }

        static public Image ImgReduceCutOut(Image SrcImg, int DesWidth, int DesHeight, bool Cut = true)
        {
            int SrcWidth = SrcImg.Width;
            int SrcHeight = SrcImg.Height;

            int ScaleWidth = 0; // 缩放宽度
            int ScaleHeight = 0; // 缩放高度

            // ＝＝＝获得缩小，裁剪大小＝＝＝
            if (Cut)
            {
                if (SrcHeight * DesWidth / SrcWidth > DesHeight)
                {
                    ScaleWidth = DesWidth;
                    ScaleHeight = SrcHeight * DesWidth / SrcWidth;
                }
                else if (SrcWidth * DesHeight / SrcHeight > DesWidth)
                {
                    ScaleWidth = SrcWidth * DesHeight / SrcHeight;
                    ScaleHeight = DesHeight;
                }
                else
                {
                    ScaleWidth = DesWidth;
                    ScaleHeight = DesHeight;
                }
            }
            else
            {
                if (SrcWidth > DesWidth)
                {
                    ScaleWidth = DesWidth;
                    ScaleHeight = SrcHeight * DesWidth / SrcWidth;
                }
                else if (SrcHeight > DesHeight)
                {
                    ScaleWidth = SrcWidth * DesHeight / SrcHeight;
                    ScaleHeight = DesHeight;
                }
                else
                {
                    ScaleWidth = SrcWidth;
                    ScaleHeight = SrcHeight;
                }
            }
            // ＝＝＝缩小图片＝＝＝

            Bitmap oldBitmap = new Bitmap(SrcImg);
            Bitmap ScaleBitmap = new Bitmap(ScaleWidth, ScaleHeight);
            Graphics grap = Graphics.FromImage(ScaleBitmap);
            Rectangle oldRect = new Rectangle(0, 0, oldBitmap.Width, oldBitmap.Height);
            Rectangle newRect = new Rectangle(0, 0, ScaleWidth, ScaleHeight);
            grap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            grap.DrawImage(oldBitmap, newRect, oldRect, GraphicsUnit.Pixel);
            oldBitmap.Dispose();
            if (Cut)
            {
                // ＝＝＝裁剪图片＝＝＝
                Rectangle DesRect = new Rectangle((ScaleWidth - DesWidth) / 2, (ScaleHeight - DesHeight) / 2, DesWidth, DesHeight);
                PixelFormat format = ScaleBitmap.PixelFormat;
                Bitmap DesBitmap = ScaleBitmap.Clone(DesRect, format);
                ScaleBitmap.Dispose();
                return DesBitmap;
            }
            else
            {
                return ScaleBitmap;
            }
        }
    }
}